package com.syni.mdd.yhd.common.all.service.impl;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxConfigDao;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxGroupBuyDao;
import com.syni.mdd.yhd.common.all.dto.reponseDto.config.GoldRpDto;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.config.BmsDxConfig;
import com.syni.mdd.yhd.common.all.service.BmsDxConfigService;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.vo.rp.config.GetAppConfigRpVo;
import com.syni.mdd.yhd.common.all.vo.rp.config.GoldRpVo;
import com.syni.mdd.yhd.common.all.vo.rp.config.GroupbuysRpVp;
import com.syni.mdd.yhd.common.all.vo.rp.miniappConfig.GetMiniAppConfigRpVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddDistSys.FxCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
@RefreshScope
public class BmsDxConfigServiceImpl implements BmsDxConfigService {
    @Autowired
    BmsDxConfigDao bmsDxConfigDao;

    @Autowired
    BmsDxGroupBuyDao bmsDxGroupBuyDao;

    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;

    @Autowired
    FxCenterService fxCenterService;

    @Value("${app.is.close}")
    private Integer isClose;


    @Override
    public BmsDxConfigDao getBaseDao() {
        return bmsDxConfigDao;
    }

    @Override
    public String getConfigByCode(int mealCode) {
        BmsDxConfig bmsDxConfig = bmsDxConfigDao.findByCodes(mealCode);
        if(bmsDxConfig != null){
            return bmsDxConfig.getConfig();
        }
        return null;
    }

    @Override
    public Map<String, Object> getMiniAppConfig(ParamsMap paramsMap) {
        String bannerConfig = getConfigByCode(MINAPP_BANNER_CODE);
        GetMiniAppConfigRpVo getMiniAppConfigRpVo = new GetMiniAppConfigRpVo();
        if(StringUtils.isNotBlank(bannerConfig)){
            List<Map> bannerList = JSON.parseArray(bannerConfig,Map.class);
            if(bannerList != null && bannerList.size() > 0){
                for(Map map : bannerList){
                    getMiniAppConfigRpVo.getIndexBanners().add(map);
                }
            }
        }
        String regionConfig = getConfigByCode(MINAPP_REGION_CODE);
        if(StringUtils.isNotBlank(regionConfig)){
            Map regionListMap = (Map) JSON.parse(regionConfig);
            List<BmsDxGroupBuy> bmsDxGroupBuyList = new ArrayList<>();
            if(regionListMap.get("isShow").equals(Constants.COMMON_FLAG_TRUE_INT) && regionListMap.containsKey("groupIds")){
                List<Integer> groupIds = JSON.parseArray(regionListMap.get("groupIds").toString(),Integer.class);
                groupIds.forEach(v->{
                    BmsDxGroupBuy bmsDxGroupBuy = bmsDxGroupBuyDao.findByIdAndIsDelete(v, Constants.COMMON_FLAG_FALSE_INT);
                    if(bmsDxGroupBuy != null && bmsDxGroupBuy.getStatus() == Constants.COMMON_FLAG_TRUE_INT){
                        if(bmsDxGroupBuy.getIsTimeLimit() == Constants.COMMON_FLAG_TRUE_INT){
                            pushResultTime(bmsDxGroupBuy);
                        }
                        bmsDxGroupBuyList.add(bmsDxGroupBuy);
                    }
                });
            }
            regionListMap.remove("groupIds");
            bmsDxGroupBuyService.postHandleBusiness(bmsDxGroupBuyList);
            List<GroupbuysRpVp> groupbuysRpVps = BeanUtils.copyList(bmsDxGroupBuyList,GroupbuysRpVp.class);
            regionListMap.put("groupbuys",groupbuysRpVps);
            getMiniAppConfigRpVo.getRegions().putAll(regionListMap);
            getMiniAppConfigRpVo.getRegions().put("serverTime",new Date());
            getMiniAppConfigRpVo.getRegions().put("count",bmsDxGroupBuyList.size());
        }
        String goldConfig = getConfigByCode(MDD_GOLD_CODE);
        if(StringUtils.isNotBlank(goldConfig)) {
            List<GoldRpDto> goldListMap = (List<GoldRpDto>)JSON.parseArray(goldConfig,GoldRpDto.class);
            List<GoldRpVo> goldListRpVo = (List<GoldRpVo>)JSON.parseArray(JSON.toJSONString(goldListMap),GoldRpVo.class);
            Collections.sort(goldListRpVo, new Comparator<GoldRpVo>() {
                @Override
                public int compare(GoldRpVo o1, GoldRpVo o2) {
                   Integer  i1 = o1.getIndex();
                   Integer i2 = o2.getIndex();
                    return i1.compareTo(i2);
                }
            });
            getMiniAppConfigRpVo.setGoldConfig(goldListRpVo);
        }
        return ResultMapHelper.success(getMiniAppConfigRpVo, ResultMsg.MSG_GET_SUCCESS);
    }

    // 添加限时时间 天时分
    public void pushResultTime(BmsDxGroupBuy bmsDxGroupBuy){
        //抢购结束时间
        Date limitBuyTime =  bmsDxGroupBuy.getLimitBuyTime();
        //当前服务器时间毫秒值
        Date serverTime = new Date();
        long mssLong = limitBuyTime.getTime() - serverTime.getTime();
        System.out.println("limitBuyTime.getTime()"+limitBuyTime.getTime());
        System.out.println("serverTime.getTime()"+serverTime.getTime());
        System.out.println("mssLong"+mssLong);
        //天
        long days = mssLong/(1000*60*60*24);
        //时
        long hours = (mssLong % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        //分
        long minutes =  (mssLong % (1000 * 60 * 60)) / (1000 * 60);
        if(days == 0){
            bmsDxGroupBuy.setRobLeftDay("00");
        }else if(0<days&&days<10){
            bmsDxGroupBuy.setRobLeftDay("0"+days);
        }else{
            bmsDxGroupBuy.setRobLeftDay(String.valueOf(days));
        }
        if(hours == 0){
            bmsDxGroupBuy.setRobLeftHour("00");
        }else if(0<hours&&hours<10){
            bmsDxGroupBuy.setRobLeftHour("0"+hours);
        }else{
            bmsDxGroupBuy.setRobLeftHour(String.valueOf(hours));
        }
        if(minutes == 0){
            bmsDxGroupBuy.setRobLeftMinute("00");
        }else if(0<minutes&&minutes<10){
            bmsDxGroupBuy.setRobLeftMinute("0"+minutes);
        }else{
            bmsDxGroupBuy.setRobLeftMinute(String.valueOf(minutes));
        }
        System.out.println(String.format("%s天%s时%s分",days,hours,minutes));
    }


    @Override
    public Map<String, Object> getDxshAppConfig(ParamsMap paramsMap) {
        String bannerConfig = getConfigByCode(DXSHAPP_CONFIG_CODE);
        Map<String,Object> resultMap = new HashMap<>();
        if(StringUtils.isNotBlank(bannerConfig)){
            resultMap.putAll(JSON.parseObject(bannerConfig));
        }
        return ResultMapHelper.success(resultMap,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> getAppConfig(ParamsMap paramsMap) {
        GetAppConfigRpVo getAppConfigRpVo = new GetAppConfigRpVo();
        String goldConfig = getConfigByCode(MDD_GOLD_CODE);
        if(StringUtils.isNotBlank(goldConfig)) {
            List<GoldRpDto> goldListMap = (List<GoldRpDto>)JSON.parseArray(goldConfig,GoldRpDto.class);
            List<GoldRpVo> goldListRpVo = (List<GoldRpVo>)JSON.parseArray(JSON.toJSONString(goldListMap),GoldRpVo.class);
            Collections.sort(goldListRpVo, new Comparator<GoldRpVo>() {
                @Override
                public int compare(GoldRpVo o1, GoldRpVo o2) {
                    Integer  i1 = o1.getIndex();
                    Integer i2 = o2.getIndex();
                    return i1.compareTo(i2);
                }
            });
            getAppConfigRpVo.setGoldConfig(goldListRpVo);
        }
        return ResultMapHelper.success(getAppConfigRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    @Transactional
    public void okc() {
        BmsDxConfig bmsDxConfig = new BmsDxConfig();
        bmsDxConfig.setCodes(123);
        bmsDxConfig.setDescText("1564");
        bmsDxConfig.setNewTime(new Date());
        bmsDxConfig.setConfig("agg");
        bmsDxConfigDao.save(bmsDxConfig);
        int i = 7 / 0;
    }

    @Override
    public Map<String, Object> getAppInitConfig(ParamsMap paramsMap) {
        //CheckUserBo checkUserBo = com.syni.mdd.yhd.common.all.utils.RequestParamUtils.checkUserBo();
        String regionConfig = getConfigByCode(MDD_APP_INIT);
        Integer userId = paramsMap.getInteger("userId");
        Integer fxRole = 0;
        if(userId != null){
            fxRole = fxCenterService.getFxRole(userId);
        }
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> fxMap = new HashMap<>();
        fxMap.put("fxRole",fxRole);
        params.put("fxSys",fxMap);
        params.put("isClose",isClose);
        return ResultMapHelper.success(params,"获取成功");
    }
}
